package com.team.service.impl;

import com.team.dto.DocumentDto;
import com.team.entity.Competition;
import com.team.entity.DocumentReview;
import com.team.mapper.CompetitionMapper;
import com.team.mapper.DocumentMapper;
import com.team.mapper.TeamMapper;
import com.team.mapper.UserMapper;
import com.team.service.DocumentService;
import com.team.vo.DocumentReviewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DocumentServiceImpl implements DocumentService {
    @Autowired
    private DocumentMapper documentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private CompetitionMapper competitionMapper;

    @Override
    public List<DocumentReviewVo> getAllApprovals() {
        return documentMapper.getAllApprovals();
    }

    @Override
    public void updateDocumentStatus(int studentId, int status,int did) {
        documentMapper.updateDocumentStatus(studentId,status,did);

        // 当 status = 1 时，更新 competition 的 stage
        if (status == 1) {
            int cid = competitionMapper.getCidByDid(did);
            // 更新stage字段
            competitionMapper.updateCompetitionStage(cid);
        }
    }

    @Override
    public void addDocumentApproval(DocumentDto documentDto) {
        //在文档的记录表中插入
        documentMapper.insertDocument(documentDto);
        //获取插入之后的did
        int did=documentDto.getId();
        //在文档申请中插入，需要注意的是文档审核中的student_id是审核人的id，所以暂写为空

        int tid = teamMapper.getTid(documentDto.getStudentId());
        //申请表中的student_id是申请人的id
        String applicant = teamMapper.getName(tid);
        String name=documentDto.getName();
        DocumentReview documentReview = DocumentReview.builder()
                .did(did).name(name).applicant(applicant).time(documentDto.getTime()).status(2).build();
        documentMapper.insertDocumentApproval(documentReview);

        // TODO 在competitions表中插入信息(url待传 此时是默认的）
        String url="\"<img src=\\\"https://www.helloimg.com/i/2025/01/06/677b7a32b66c3.jpg\\\" alt=\\\"default-合照&logo.jpg\\\" title=\\\"default-合照&logo.jpg\\\" />\"";
        // 创建competition对象并插入
        Competition competition =Competition.builder().tid(tid).did(did).name(name).cate(documentDto.getCate())
                .time(documentDto.getStartTime()).format(documentDto.getFormat()).stage("申报阶段").entrants(documentDto.getEntrants())
                        .organizers(documentDto.getOrganizers()).entrantsSum(0).firstChoicesSum(0).imgSrc(url).isTop(true).build();
        // 插入competitions表
        competitionMapper.insert(competition);
    }

    @Override
    public List<DocumentReviewVo> getMyApprovals(int applicantId) {
        //获得队长管理的团队的tid
        Integer tid=teamMapper.getTid(applicantId);

        //获得团队名称
        String teamName=teamMapper.getName(tid);

        return documentMapper.getMyApprovals(teamName);
    }
}
